package com.dubbo.comsumer;

import com.dubbo.protocol.Client;
import com.dubbo.protocol.http.HttpClient;
import com.dubbo.protocol.bean.Invocation;
import com.dubbo.register.RemoteRegister;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.net.URL;

public class RpcClientProxy
{
    public static  <T> T  newInstance(URL url, Class clazz, Client client)
    {
        return (T)Proxy.newProxyInstance(clazz.getClassLoader(), new Class[]{clazz}, new InvocationHandler()
        {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
            {
                Invocation invocation = new Invocation();
                invocation.setInterfaceName(clazz.getName());
                invocation.setMethodName(method.getName());
                invocation.setParamsClass(method.getParameterTypes());
                invocation.setParamsValue(args);

                System.out.println("client send invocation:" + invocation);


                return client.send(url,invocation);
            }
        });
    }
}
